package com.audaque.springboot.foshanupload.web.introspector;


import com.audaque.springboot.foshanupload.core.anno.DataMaskAnno;
import com.audaque.springboot.foshanupload.core.anno.DictAnno;
import com.audaque.springboot.foshanupload.core.serializer.DataMaskSerializer;
import com.audaque.springboot.foshanupload.core.serializer.DictSerializer;
/*import com.audaque.springboot.foshanupload.jwtcore.anno.RsaAnno;
import com.audaque.springboot.foshanupload.jwtcore.serializer.RsaSerializer;*/
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zgb
 * @desc NopAnnotationIntrospector：字面意思 “注释内省器”
 * @date 2022-11-22 20:17:42
 */
@Slf4j
public class ObjectMapperAnnoIntrospector extends NopAnnotationIntrospector {

    @SneakyThrows
    @Override
    public Object findSerializer(Annotated am) {
        DataMaskAnno dataMaskAnno = am.getAnnotation(DataMaskAnno.class);
        //RsaAnno rsaAnno =null;
        // rsaAnno = am.getAnnotation(RsaAnno.class);
        DictAnno dictAnno = am.getAnnotation(DictAnno.class);
        int count = 0;
        if (dataMaskAnno != null) {
            count++;
        }
       /* if (rsaAnno != null) {
            count++;
        }*/
        if (dictAnno != null) {
            count++;
        }
        if (count > 1) {
            /*      try {*/
            throw new Exception("ObjectMapperAnn只能使用一个");
  /*          } catch (Exception e) {
                throw new RuntimeException(e);
            }*/
        }
        if (dataMaskAnno != null) {
            //实例化 数据脱密序列化对象，入参：获得注解属性的数据脱密函数枚举对象>获取脱密函数
            return new DataMaskSerializer(dataMaskAnno.dataMaskFunctionEnum().getDataMaskFunction());
        }

      /*  if (rsaAnno != null) {
            //实例化 数据Rsa序列化对象，入参：加密或解密
            //return new RsaSerializer(rsaAnno.enOrde());
        }
*/
        if (dictAnno != null) {
            //只返回字节码对象，实例化交给序列化类自己处理,需要实现implements ContextualSerializer,把字段转为json
            return DictSerializer.class;
        }
        return null;
    }

}
